//
//  XMShareWechatUtil.m
//  XMShare
//
//  Created by Amon on 15/8/6.
//  Copyright (c) 2015年 GodPlace. All rights reserved.
//

#import "XMShareWechatUtil.h"
#import "WXApi.h"

@implementation XMShareWechatUtil
- (void)shareToWeixinSession{
    [self shareToWeixinBase:WXSceneSession];
}

- (void)shareToWeixinTimeline{
    [self shareToWeixinBase:WXSceneTimeline];
}

- (void)shareToWeixinBase:(enum WXScene)scene{
    WXMediaMessage *message = [WXMediaMessage message];
//    message.title = self.shareTitle;
//    message.description = self.shareText;
//    [message setThumbImage:self.shareImage];
//
//    WXWebpageObject *ext = [WXWebpageObject object];
//    ext.webpageUrl = self.shareUrl;
    
    WXImageObject *obj = [WXImageObject object];
    obj.imageData = [XMShareWechatUtil zf_compressWithLengthLimit:self.shareImage];
    
    message.mediaObject = obj;
    message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    [WXApi sendReq:req completion:nil];
}

+ (instancetype)sharedInstance{
    static XMShareWechatUtil* util;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        util = [[XMShareWechatUtil alloc] init];
    });
    return util;
}

- (instancetype)init{
    static id obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [super init];
        if (obj) {
        }
    });
    self = obj;
    return self;
}

//二分法
+ (NSData *)zf_compressWithLengthLimit:(UIImage *)image {
    CGFloat maxLength = 25600;
    // Compress by quality
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    //STPLog(@"Before compressing quality, image size = %ld KB",data.length/1024);
    if (data.length < maxLength) return data;

    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        //STPLog(@"Compression = %.1f", compression);
        //STPLog(@"In compressing quality loop, image size = %ld KB", data.length / 1024);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
//    STPLog(@"After compressing quality, image size = %ld KB", data.length / 1024);
    if (data.length < maxLength) return data;
    UIImage *resultImage = [UIImage imageWithData:data];
    // Compress by size
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        //STPLog(@"Ratio = %.1f", ratio);
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, compression);
        NSLog(@"In compressing size loop, image size = %ld KB", data.length / 1024);
    }
    NSLog(@"After compressing size loop, image size = %ld KB", data.length / 1024);
    return data;
}


@end
